package com.dys.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @className: com.dys.servlet.ServletCookie2
 * @description: 读取Cookie
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/20 21:11
 */
@WebServlet(urlPatterns = "/servlet02.do")
public class ServletCookie2 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行ServletCookie2中的方法......");
        // 读取请求中的Cookie
        Cookie[] cookies = req.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "=" + cookie.getValue());
            }
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().println("读取请求中的Cookie成功");
        }
    }
}
